package com.perfect.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/nacos")
@Slf4j
public class EchoController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/test")
    public void test() {
        log.info("test........");
    }

    /**
     * 测试RestTemplate 发起服务调用
     */
    @RequestMapping("/nacos/test")
    public void nacosRestTest() {

        //使用loadBalancerClient 和 restTemplate 组合的方式发起调用
        String appName = "nacos/discovery/test";
        ServiceInstance serviceInstance = loadBalancerClient.choose("goods-service");
        String url = String.format("http://%s:%s/goods/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
        restTemplate.getForEntity(url, null);
    }

    /**
     * 被访问，通过nacos 完成
     */
    @RequestMapping("/resttemplate/loadbalance/annotation")
    public void resttemplateLoadbalanceAnnotation(){
        log.info("@LoadBalance 注解调用接口成功");
        log.info("RestTemplate 。。。。。。。。。。");
    }

    /**
     * feign 和 nacos 整合访问接口
     */
    @RequestMapping("/feign/test")
    public void feignTest(){
        log.info("nacos 整合 feign 服务调用示例。。。。。。succeefully");
    }
}
